<!-- 播放页面 -->
<template>
	<view>
		<view class="play" @click="closeDrawer">
			<!-- 模糊背景(靠样式设置), 固定定位 -->
			<view class="song-bg" :style="{backgroundImage: `url(${songInfo && songInfo.al && songInfo.al.picUrl}?imageView&thumbnail=360y360&quality=75&tostatic=0)`}"></view>
			<!-- 播放页头部导航 -->
			<view class="header" :style="'margin-top:'+ (titletop+titleheight/2-10)+'px'">
				<u-icon name="arrow-left" color="#fff" size="20"  @click="back" />
			</view>
			<!-- 留声机 - 容器 -->
			<view class="song-wrapper" :style="'top:'+ (titletop+titleheight+statusBarHeight+40)+'px'">
				<!-- 留声机本身(靠css动画做旋转) -->
				<view class="song-turn ani" :style="{animationPlayState :`${playState ? 'running' : 'paused'}` }">
					<view class="song-img">
						<!-- &&写法是为了防止报错, 有字段再继续往下访问属性 -->
						<image style="height: 100%" :src="`${songInfo && songInfo.al && songInfo.al.picUrl}?imageView&thumbnail=360y360&quality=75&tostatic=0`" alt="" />
					</view>
				</view>
				<!-- 播放按钮 -->
				<view class="start-box" @click="audioStart">
					<span class="song-start" v-show="!playState"></span>
				</view>
				<!-- 播放歌词容器 -->
				<view class="song-msg">
					<!-- 歌曲名 -->
					<h2 class="m-song-h2">
						<span class="m-song-sname">{{ songInfo.name }}-{{ songInfo && songInfo.ar && songInfo.ar[0].name }}</span>
					</h2>
					<!-- 歌词部分-随着时间切换展示一句歌词 -->
					<view class="lrcContent">
						<p class="lrc">{{ curLyric }}</p>
					</view>
				</view>
				<!-- 留声机 - 唱臂 -->
				<view class="needle" :style="{transform: `rotate(${needleDeg})`}"></view>
			</view>
		</view>
		<DrawerView ref="drawer"></DrawerView>
	</view>
</template>

<script>
	// 获取歌曲详情和 歌曲的歌词接口
	import { getSongByIdAPI, getLyricByIdAPI, } from '@/api'
	import DrawerView from '@/components/DrawerView.vue'
	export default {
		components: {
			DrawerView,
		},
		name: 'play',
		data() {
			return {
				playState: false, // 音乐播放状态(true暂停, false播放)
				id: '', // 上一页传过来的音乐id
				songInfo: {}, // 歌曲信息
				lyric: {}, // 歌词枚举对象(需要在js拿到歌词写代码处理后, 按照格式保存到这个对象)
				curLyric: '', // 当前显示哪句歌词
				lastLy: '', // 记录当前播放歌词
				
				titletop:0,
				titleheight:0,
				statusBarHeight: 0,
			}
		},
		computed: {
			needleDeg() {
				// 留声机-唱臂的位置属性
				return this.playState ? '-7deg' : '-38deg'
			},
		},
		onLoad(option) {
			this.id = option.id
			getApp().globalData.audioMusic.src = `https://music.163.com/song/media/outer/url?id=${this.id}.mp3`
			this.getSong()
			this.showLyric()
			// 调用APP中的数据
			getApp().getHeight();
			this.titletop = getApp().titletop;
			this.titleheight = getApp().titleheight;
			this.statusBarHeight = getApp().statusBarHeight;
		},
		mounted() {
			this.$refs.drawer.id = this.id
			this.$refs.drawer.Related()
		},
		methods: {
			// 关闭子组件抽屉
			closeDrawer() {
				this.$refs.drawer.close()
			},
			// 返回上一页
			back() {
				uni.navigateBack()
			},
			async getSong() {
				// 获取歌曲详情, 和歌词方法
				const res = await getSongByIdAPI(this.id)
				this.songInfo = res.songs[0]
				// 获取-并调用_formatLyr方法, 处理歌词
				const lyrContent = await getLyricByIdAPI(this.id)
				const lyricStr = lyrContent.lrc.lyric
				this.lyric = this._formatLyr(lyricStr)
				// 初始化完毕先显示零秒歌词
				this.curLyric = this.lyric[0]
				await this.audioStart()
				getApp().globalData.audioMusic.title = this.songInfo?.name;
				getApp().globalData.audioMusic.singer = this.songInfo && this.songInfo.ar && this.songInfo.ar[0].name;
				getApp().globalData.audioMusic.coverImgUrl = `${this.songInfo && this.songInfo.al && this.songInfo.al.picUrl}?imageView&thumbnail=360y360&quality=75&tostatic=0`;
			},
			_formatLyr(lyricStr) {
				// 可以看network观察歌词数据是一个大字符串, 进行拆分.
				let reg = /\[.+?\]/g //
				let timeArr = lyricStr.match(reg) // 匹配所有[]字符串以及里面的一切内容, 返回数组
				let contentArr = lyricStr.split(/\[.+?\]/).slice(1) // 按照[]拆分歌词字符串, 返回一个数组(下标为0位置元素不要,后面的留下所以截取)
				let lyricObj = {} // 保存歌词的对象, key是秒, value是显示的歌词
				timeArr.forEach((item, index) => {
					// 拆分[00:00.000]这个格式字符串, 把分钟数字取出, 转换成秒
					let ms = item.split(':')[0].split('')[2] * 60
					// 拆分[00:00.000]这个格式字符串, 把十位的秒拿出来, 如果是0, 去拿下一位数字, 否则直接用2位的值
					let ss = item.split(':')[1].split('.')[0].split('')[0] === '0' ? item.split(':')[1].split('.')[0].split('')[1] : item.split(':')[1].split('.')[0]
					// 秒数作为key, 对应歌词作为value
					lyricObj[ms + Number(ss)] = contentArr[index]
				})
				// 返回得到的歌词对象(可以打印看看)
				return lyricObj
			},
			audioStart() {
				// 播放按钮 - 点击事件
				if (!this.playState) {
					// 如果状态为false
					getApp().globalData.audioMusic.play() // 调用audio标签的内置方法play可以继续播放声音
				} else {
					getApp().globalData.audioMusic.pause() // 暂停audio的播放
				}
				this.playState = !this.playState // 点击设置对立状态
			},
			showLyric() {
				// 监听播放audio进度, 切换歌词显示
				getApp().globalData.audioMusic.onTimeUpdate(() => {
					let curTime = Math.floor(getApp().globalData.audioMusic.currentTime)
					// 避免空白出现
					if (this.lyric[curTime]) {
						this.curLyric = this.lyric[curTime]
						this.lastLy = this.curLyric
					} else {
						this.curLyric = this.lastLy
					}
				})
			},
		}
	}
</script>

<style scoped>
	.header {
    position: absolute;
		left: 10px;
		z-index: 100;
	}

	.play {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 60px;
	}

	.song-bg {
		background-color: #161824;
		background-position: 50%;
		background-repeat: no-repeat;
		background-size: auto 100%;
		transform: scale(1.5);
		transform-origin: center;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		height: 100%;
		overflow: hidden;
		z-index: 1;
		opacity: 1;
		filter: blur(25px); /*模糊背景 */
	}

	.song-bg::before {
		/*纯白色的图片做背景, 歌词白色看不到了, 在背景前加入一个黑色半透明蒙层解决 */
		content: ' ';
		background: rgba(0, 0, 0, 0.5);
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
	}

	.song-wrapper {
		position: fixed;
		width: 247px;
		height: 247px;
		left: 50%;
		transform: translateX(-50%);
		z-index: 10001;
	}

	.song-turn {
		width: 100%;
		height: 100%;
		background: url('https://p5.music.126.net/obj/wo3DlcOGw6DClTvDisK1/4602256867/5855/63fb/325b/d7e4e3a244701ee85fecb5d4f6b5bd57.png?imageView=&quality=75&thumbnail=200y200') no-repeat;
		background-size: 100%;
	}

	.start-box {
		position: absolute;
		width: 156px;
		height: 156px;
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.song-start {
		width: 56px;
		height: 56px;
		background: url('');
		background-size: 100%;
	}

	.needle {
		position: absolute;
		transform-origin: left top;
		background: url('https://s3.music.126.net/mobile-new/img/needle-ab.png?4b81da043356110081e5ccaed2e6f2ad=') no-repeat;
		background-size: contain;
		width: 73px;
		height: 118px;
		top: -40px;
		left: 112px;
		transition: all 0.6s;
	}

	.song-img {
		width: 154px;
		height: 154px;
		position: absolute;
		left: 50%;
		top: 50%;
		overflow: hidden;
		border-radius: 50%;
		transform: translate(-50%, -50%);
	}

	.m-song-h2 {
		margin-top: 20px;
		text-align: center;
		font-size: 18px;
		color: #fefefe;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.lrcContent {
		margin-top: 50px;
	}

	.lrc {
		font-size: 14px;
		color: #fff;
		text-align: center;
	}

	.left-incon {
		position: absolute;
		top: 10px;
		left: 10px;
		font-size: 24px;
		z-index: 10001;
		color: #fff;
	}

	.ani {
		animation: turn 20s linear infinite;
	}

	@keyframes turn {
		0% {
			-webkit-transform: rotate(0deg);
		}

		100% {
			-webkit-transform: rotate(360deg);
		}
	}
</style>